package edu.kit.ksri.db.mturk.utils;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class MathUtils {

	/**
	 * 
	 * @return the maximum value or null if the array is empty or null
	 */
	public static Double max(Double[] d) {
		if (d == null || d.length == 0)
			return null;
		double[] d2 = new double[d.length];
		for (int i = 0; i < d.length; i++)
			d2[i] = d[i];
		return max(d2);
	}

	/**
	 * 
	 * @return the maximum value or null if the array is empty or null
	 */
	public static Double max(double[] d) {
		if (d == null || d.length == 0)
			return null;
		if (d.length == 1)
			return d[0];

		double max = d[0];
		for (double dd : d) {
			if (dd > max)
				max = dd;
		}
		return max;
	}

	/**
	 * 
	 * @return the maximum value or null if the array is empty or null
	 */
	public static Integer max(Integer[] d) {
		if (d == null || d.length == 0)
			return null;
		int[] d2 = new int[d.length];
		for (int i = 0; i < d.length; i++)
			d2[i] = d[i];
		return max(d2);
	}

	/**
	 * 
	 * @return the maximum value or null if the array is empty or null
	 */
	public static Integer max(int[] d) {
		if (d == null || d.length == 0)
			return null;
		if (d.length == 1)
			return d[0];

		int max = d[0];
		for (int dd : d) {
			if (dd > max)
				max = dd;
		}
		return max;
	}

	/**
	 * 
	 * @return the minimum value or null if the array is empty or null
	 */
	public static Double min(Double[] d) {
		if (d == null || d.length == 0)
			return null;
		double[] d2 = new double[d.length];
		for (int i = 0; i < d.length; i++)
			d2[i] = d[i];
		return min(d2);
	}

	/**
	 * 
	 * @return the minimum value or null if the array is empty or null
	 */
	public static Double min(double[] d) {
		if (d == null || d.length == 0)
			return null;
		if (d.length == 1)
			return d[0];

		double min = d[0];
		for (double dd : d) {
			if (dd < min)
				min = dd;
		}
		return min;
	}

	/**
	 * 
	 * @return the minimum value or null if the array is empty or null
	 */
	public static Integer min(Integer[] d) {
		if (d == null || d.length == 0)
			return null;
		int[] d2 = new int[d.length];
		for (int i = 0; i < d.length; i++)
			d2[i] = d[i];
		return min(d2);
	}

	/**
	 * 
	 * @return the minimum value or null if the array is empty or null
	 */
	public static Integer min(int[] d) {
		if (d == null || d.length == 0)
			return null;
		if (d.length == 1)
			return d[0];

		int min = d[0];
		for (int dd : d) {
			if (dd < min)
				min = dd;
		}
		return min;
	}

	/**
	 * 
	 * @param d
	 * @return the average value or null if the array is empty or null
	 */
	public static Double avg(Double[] d) {
		if (d == null || d.length == 0)
			return null;
		double[] d2 = new double[d.length];
		for (int i = 0; i < d.length; i++)
			d2[i] = d[i];
		return avg(d2);
	}

	/**
	 * 
	 * @param d
	 * @return the average value or null if the array is empty or null
	 */
	public static Double avg(double[] d) {
		if (d == null || d.length == 0)
			return null;
		double sum = 0;
		for (double dd : d)
			sum += dd;
		return sum / d.length;
	}

	/**
	 * 
	 * @param d
	 * @return the average value or null if the array is empty or null
	 */
	public static Double avg(Integer[] d) {
		if (d == null || d.length == 0)
			return null;
		double[] d2 = new double[d.length];
		for (int i = 0; i < d.length; i++)
			d2[i] = d[i];
		return avg(d2);
	}

	/**
	 * 
	 * @param d
	 * @return the average value or null if the array is empty or null
	 */
	public static Double avg(int[] d) {
		if (d == null || d.length == 0)
			return null;
		double sum = 0;
		for (double dd : d)
			sum += dd;
		return sum / d.length;
	}

}
